{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 10.5 实现池化\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设一张图像的大小为4×4×1，其像素矩阵如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "fda35add-808b-436a-a224-ea33268deb25",
   "metadata": {},
   "source": [
    "[[2,1,0,2],\n",
    "[9,5,4,2],\n",
    "[2,3,4,5],\n",
    "[1,2,3,1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3903b806-5cb1-4796-b785-eb466876dec8",
   "metadata": {},
   "source": [
    "要求：\n",
    "\n",
    "- 使用一个步长为2的2×2的池化核，对图像进行最大值池化操作，并输出计算结果"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55043130-4496-43a3-803b-9bc1cea8b1b8",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "通过调用tf.nn.max_pool2d方法可以实现对张量的最大值池化。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入形状: (1, 4, 4, 1)\n",
      "输出形状: (1, 2, 2, 1)\n",
      "输出:\n",
      "[[9. 4.]\n",
      " [3. 5.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# 1，定义输入\n",
    "# 原始输入\n",
    "input=tf.constant([[2,1,0,2],\n",
    "                     [9,5,4,2],\n",
    "                     [2,3,4,5],\n",
    "                     [1,2,3,1]],dtype=tf.float32)\n",
    "# 进行形状转换（增加批维度）\n",
    "input=tf.expand_dims(input,0)\n",
    "# 进行形状转换（增加通道维度）\n",
    "input=tf.expand_dims(input,3)\n",
    "print(\"输入形状:\",input.shape)\n",
    "# 2，进行最大值池化\n",
    "out=tf.nn.max_pool2d(\n",
    "    # 输入\n",
    "    input=input, \n",
    "    # 池化核大小\n",
    "    ksize=(2,2), \n",
    "    # 步长\n",
    "    strides=2,\n",
    "    # 填充\n",
    "    padding='VALID')\n",
    "print(\"输出形状:\",out.shape)\n",
    "# 3，删除长度是1的维度\n",
    "print(\"输出:\")\n",
    "out=tf.squeeze(out)\n",
    "print(out.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
